{
  "cells": [
    {
      "cell_type": "markdown",
      "id": "cell_0",
      "metadata": {},
      "source": [
        "# ContextGem: RatingConcept Extraction"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": null,
      "id": "cell_1",
      "metadata": {},
      "outputs": [],
      "source": [
        "%pip install -U contextgem"
      ]
    },
    {
      "cell_type": "markdown",
      "id": "cell_2",
      "metadata": {},
      "source": [
        "To run the extraction, please provide your LLM details in the ``DocumentLLM(...)`` constructor further below."
      ]
    },
    {
      "cell_type": "code",
      "execution_count": null,
      "id": "cell_3",
      "metadata": {},
      "outputs": [],
      "source": [
        "# ContextGem: RatingConcept Extraction\n",
        "\n",
        "import os\n",
        "\n",
        "from contextgem import Document, DocumentLLM, RatingConcept\n",
        "\n",
        "\n",
        "# Create a Document object from text describing a product without an explicit rating\n",
        "smartphone_description = (\n",
        "    \"This smartphone features a 5000mAh battery that lasts all day with heavy use. \"\n",
        "    \"The display is 6.7 inch AMOLED with 120Hz refresh rate. \"\n",
        "    \"Camera system includes a 50MP main sensor, 12MP ultrawide, and 8MP telephoto lens. \"\n",
        "    \"The phone runs on the latest processor with 8GB RAM and 256GB storage. \"\n",
        "    \"It has IP68 water resistance and Gorilla Glass Victus protection.\"\n",
        ")\n",
        "\n",
        "doc = Document(raw_text=smartphone_description)\n",
        "\n",
        "# Define a RatingConcept that requires analysis to determine a rating\n",
        "product_quality = RatingConcept(\n",
        "    name=\"Product Quality Rating\",\n",
        "    description=(\n",
        "        \"Evaluate the overall quality of the smartphone based on its specifications, \"\n",
        "        \"features, and adherence to industry best practices\"\n",
        "    ),\n",
        "    rating_scale=(1, 10),\n",
        "    add_justifications=True,  # include justification for the rating\n",
        "    justification_depth=\"balanced\",\n",
        "    justification_max_sents=5,\n",
        ")\n",
        "\n",
        "# Attach the concept to the document\n",
        "doc.add_concepts([product_quality])\n",
        "\n",
        "# Configure DocumentLLM with your API parameters\n",
        "llm = DocumentLLM(\n",
        "    model=\"azure/gpt-4.1\",\n",
        "    api_key=os.getenv(\"CONTEXTGEM_AZURE_OPENAI_API_KEY\"),\n",
        "    api_version=os.getenv(\"CONTEXTGEM_AZURE_OPENAI_API_VERSION\"),\n",
        "    api_base=os.getenv(\"CONTEXTGEM_AZURE_OPENAI_API_BASE\"),\n",
        ")\n",
        "\n",
        "# Extract the concept from the document - the LLM will analyze and assign a rating\n",
        "product_quality = llm.extract_concepts_from_document(doc)[0]\n",
        "\n",
        "# Print the calculated rating\n",
        "print(f\"Quality Rating: {product_quality.extracted_items[0].value}\")\n",
        "# Print the justification\n",
        "print(f\"Justification: {product_quality.extracted_items[0].justification}\")\n"
      ]
    }
  ],
  "metadata": {
    "kernelspec": {
      "display_name": "Python 3",
      "language": "python",
      "name": "python3"
    },
    "language_info": {
      "codemirror_mode": {
        "name": "ipython",
        "version": 3
      },
      "file_extension": ".py",
      "mimetype": "text/x-python",
      "name": "python",
      "nbconvert_exporter": "python",
      "pygments_lexer": "ipython3",
      "version": "3.10.0"
    }
  },
  "nbformat": 4,
  "nbformat_minor": 5
}